<?php

/**
 * Radcodes - SocialEngine Module
 *
 * @category   Application_Extensions
 * @package    Cmspage
 * @copyright  Copyright (c) 2009-2010 Radcodes LLC (http://www.radcodes.com)
 * @license    http://www.radcodes.com/license/
 * @version    $Id$
 * @author     Vincent Van <vincent@radcodes.com>
 */

class Cmspage_AdminManageController extends Core_Controller_Action_Admin {
	public function indexAction() {
		$this->view->navigation = $navigation = Engine_Api::_ ()->getApi ( 'menus', 'core' )->getNavigation ( 'cmspage_admin_main', array (), 'cmspage_admin_main_manage' );
		
		$this->view->formFilter = $formFilter = new Cmspage_Form_Admin_Manage_Filter ();
		
		// Process form
		$values = array ();
		if ($formFilter->isValid ( $this->_getAllParams () )) {
			$values = $formFilter->getValues ();
		}
		$values = Engine_Api::_ ()->getApi ( 'filter', 'radcodes' )->removeKeyEmptyValues ( $values );
		
		$this->view->formValues = $values;
		
		$this->view->assign ( $values );
		
		$this->view->paginator = Engine_Api::_ ()->cmspage ()->getCmspagesPaginator ( $values );
		$this->view->paginator->setItemCountPerPage ( ( int ) Engine_Api::_ ()->getApi ( 'settings', 'core' )->getSetting ( 'cmspage.page', 10 ) );
		$this->view->paginator->setCurrentPageNumber ( $this->_getParam ( 'page', 1 ) );
		$this->view->params = $values;
	
	}
	
	public function deleteselectedAction() {
		$this->view->ids = $ids = $this->_getParam ( 'ids', null );
		$confirm = $this->_getParam ( 'confirm', false );
		$this->view->count = count ( explode ( ",", $ids ) );
		
		// Save values
		if ($this->getRequest ()->isPost () && $confirm == true) {
			$ids_array = explode ( ",", $ids );
			foreach ( $ids_array as $id ) {
				$cmspage = Engine_Api::_ ()->getItem ( 'cmspage', $id );
				if ($cmspage)
					$cmspage->delete ();
			}
			
			$this->_helper->redirector->gotoRoute ( array ('action' => 'index' ) );
		}
	
	}
	
	public function featuredAction() {
		// In smoothbox
		$this->_helper->layout->setLayout ( 'admin-simple' );
		$id = $this->_getParam ( 'id' );
		$this->view->cmspage_id = $id;
		// Check post
		if ($this->getRequest ()->isPost ()) {
			$db = Engine_Db_Table::getDefaultAdapter ();
			$db->beginTransaction ();
			
			try {
				$cmspage = Engine_Api::_ ()->getItem ( 'cmspage', $id );
				
				$cmspage->featured = $this->_getParam ( 'featured' ) == 'yes' ? 1 : 0;
				$cmspage->save ();
				
				$db->commit ();
			} 

			catch ( Exception $e ) {
				$db->rollBack ();
				throw $e;
			}
			
			$this->_forward ( 'success', 'utility', 'core', array ('smoothboxClose' => 10, 'parentRefresh' => 10, 'messages' => array ('' ) )//'messages' => array(Zend_Registry::get('Zend_Translate')->_('Changes saved'))
			 );
		}
		
		// Output
		$this->renderScript ( 'admin-manage/featured.tpl' );
	}
	
	public function stickyAction() {
		// In smoothbox
		$this->_helper->layout->setLayout ( 'admin-simple' );
		$id = $this->_getParam ( 'id' );
		$this->view->cmspage_id = $id;
		// Check post
		if ($this->getRequest ()->isPost ()) {
			$db = Engine_Db_Table::getDefaultAdapter ();
			$db->beginTransaction ();
			
			try {
				$cmspage = Engine_Api::_ ()->getItem ( 'cmspage', $id );
				
				$cmspage->sticky = $this->_getParam ( 'sticky' ) == 'yes' ? 1 : 0;
				$cmspage->save ();
				
				$db->commit ();
			} 

			catch ( Exception $e ) {
				$db->rollBack ();
				throw $e;
			}
			
			$this->_forward ( 'success', 'utility', 'core', array ('smoothboxClose' => 10, 'parentRefresh' => 10, 'messages' => array ('' ) )//'messages' => array(Zend_Registry::get('Zend_Translate')->_('Changes saved'))
			 );
		}
		
		// Output
		$this->renderScript ( 'admin-manage/sticky.tpl' );
	}
	
	public function deleteAction() {
		// In smoothbox
		$this->_helper->layout->setLayout ( 'admin-simple' );
		$id = $this->_getParam ( 'id' );
		$this->view->cmspage_id = $id;
		// Check post
		if ($this->getRequest ()->isPost ()) {
			$db = Engine_Db_Table::getDefaultAdapter ();
			$db->beginTransaction ();
			
			try {
				$cmspage = Engine_Api::_ ()->getItem ( 'cmspage', $id );
				$cmspage->delete ();
				$db->commit ();
			} 

			catch ( Exception $e ) {
				$db->rollBack ();
				throw $e;
			}
			
			$this->_forward ( 'success', 'utility', 'core', array ('smoothboxClose' => 10, 'parentRefresh' => 10, 'messages' => array ('' ) ) );
		}
		
		// Output
		$this->renderScript ( 'admin-manage/delete.tpl' );
	}
	
	public function linkAction() {
		// In smoothbox
		$this->_helper->layout->setLayout ( 'admin-simple' );
		$id = $this->_getParam ( 'id' );
		
		$this->view->cmspage = $cmspage = Engine_Api::_ ()->getItem ( 'cmspage', $id );
		
		$this->view->form = $form = new Cmspage_Form_Admin_Manage_Link ();
		$form->setField ( $cmspage );
	
		// Output
	//$this->renderScript('admin-manage/link.tpl');
	}
}